<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>监听器-02</title>
</head>
<body>
<div id="app">
    名字：<input type="text" v-model="username"/><br>
    {{msg}}
</div>
<script src="../js/vue.js" ></script>
<script>
    const app = new Vue({
        el: '#app',
        data: {
            username: '',
            msg: ''
        },
        methods: {
            checkName(val){
                if (val === 'admin'){
                    this.msg = '用户名'+val+'已存在'
                }else if(val === 'tom'){
                    this.msg = '用户名'+val+'已存在'
                }else {
                    this.msg = '可以使用'
                }
            }
        },
        watch: {
            username(val){
                this.checkName(val)
            }
        }
    })
</script>
</body>
</html>